package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.CompanyBankCard;
import cn.net.shuxing.yifu.common.bean.RechargeHistory;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.constant.CodeConst;
import cn.net.shuxing.yifu.common.constant.PaymentStatus;
import cn.net.shuxing.yifu.common.constant.UserState;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.CompanyBankCardService;
import cn.net.shuxing.yifu.common.service.RechargeHistoryService;
import cn.net.shuxing.yifu.common.service.UserService;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import cn.net.shuxing.yifu.common.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class OfflineRechargeFrontController {

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private RechargeHistoryService rechargeHistoryService;

    @Autowired
    private CompanyBankCardService companyBankCardService;

    /**
     * 发起线下充值
     * @param totalFee
     * @param userId
     * @param response
     * @return
     */
    @RequestMapping("/front/recharge/offline")
    public BaseDto offlineRecharge(String totalFee, Integer userId, HttpServletResponse response)
    {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        if (user.getState() != UserState.OK) {
            return BaseDto.error("当前状态无法发布挂卖单");
        }
        //Jedis //jedis = jedisUtil.getJedis();
        String s = jedisUtil.get(RedisKeyCreator.rechargeBaseCount());
        //jedisUtil.close();
        BigDecimal baseCount = new BigDecimal(0);
        if (StringUtils.hasText(s))
            baseCount = new BigDecimal(s);
        if (baseCount.compareTo(new BigDecimal(totalFee)) > 0)
            return BaseDto.error("充值金额至少为" + baseCount.floatValue());
        // 保存支付信息
        RechargeHistory rechargeHistory = new RechargeHistory();
        if (user == null)
            return BaseDto.error("用户不存在");
        rechargeHistory.setUserId(userId);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
        String order_no = dateFormat.format(now);  //生成商户订单号，不可重复
        rechargeHistory.setOrderNo(order_no);
        rechargeHistory.setFee(totalFee);
        CompanyBankCard defaultBankCard = companyBankCardService.getDefaultBankCard();
        if (defaultBankCard == null)
            return BaseDto.error("后台未设置默认收款银行卡");
        rechargeHistory.setSubject(JSON.toJSONString(defaultBankCard));
        rechargeHistory.setPaymentStatus(PaymentStatus.UNPAID.name());
        String outTradeNo = UUID.randomUUID().toString().substring(0,5);
        rechargeHistory.setOutTradeNo(outTradeNo);
        rechargeHistory.setType(1);

        rechargeHistoryService.insert(rechargeHistory);
        return BaseDto.ok(rechargeHistory.getId());
    }

    /**
     * 获取收款账户信息
     * @param rechargeId
     * @return
     */
    @RequestMapping("/front/recharge/offline/recieverInfo")
    public BaseDto getDefaultBankCard(Integer rechargeId)
    {
        CompanyBankCard defaultBankCard = companyBankCardService.getDefaultBankCard();
        if (defaultBankCard == null)
            return BaseDto.error("后台未设置默认收款银行卡");
        RechargeHistory rechargeHistory = rechargeHistoryService.getByPrimaryKeyWithoutComplete(rechargeId);
        String subject = rechargeHistory.getSubject();

        JSONObject jsonObject = JSON.parseObject(subject);
        jsonObject.put("extra",rechargeHistory.getOutTradeNo());
        jsonObject.put("count",rechargeHistory.getFee());
        return BaseDto.ok(jsonObject);

        /*
        {
            openBank:开户银行,
	        bankCardNum:银行卡号,
	        holderName:出卡人姓名,
	        extra: 附言,
	        count: 支付金额
        }
         */
    }
}
